package com.huatai.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.common.util.StringUtil;
import com.huatai.manage.entity.TbStageMainEntity;
import com.huatai.manage.query.PageQuery;

import com.huatai.manage.mapper.TbStageMainMapper;
import com.huatai.manage.service.TbStageMainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.manage.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 阶段管理 服务实现类
 * </p>
 *
 * @author luoxc
 * @since 2023-03-22
 */
@Service
public class TbStageMainServiceImpl extends ServiceImpl<TbStageMainMapper, TbStageMainEntity> implements TbStageMainService {

	@Autowired
	private TbStageMainMapper stageMainMapper;

	/**
	 * 分页获取父阶段节点列表
	 *
	 * @param pageQuery
	 * @return
	 */
	@Override
	public Page<TbStageMainEntity> pageList(PageQuery<TbStageMainEntity> pageQuery) {
		Page<TbStageMainEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		return stageMainMapper.pageList(page,pageQuery.getData());
	}

	/**
	 * 根据阶段id获取信息
	 *
	 * @param stageId
	 * @return
	 */
	@Override
	public TbStageMainEntity getByStageId(String stageId) {
		/*LambdaQueryWrapper<TbStageMainEntity> wrapper = Wrappers.<TbStageMainEntity>lambdaQuery();
		wrapper.eq(TbStageMainEntity::getStageId, stageId);
		return getOne(wrapper);*/
		return stageMainMapper.getByStageId(stageId);
	}

	/**
	 * 根据阶段id集合删除阶段信息
	 *
	 * @param stageIds
	 * @return
	 */
	@Override
	public boolean removes(List<String> stageIds) {
		if (stageIds.isEmpty()) return false;
		LambdaQueryWrapper<TbStageMainEntity> wrapper = Wrappers.<TbStageMainEntity>lambdaQuery();
		wrapper.in(TbStageMainEntity::getStageId, stageIds);
		return remove(wrapper);
	}

	/**
	 * 根据阶段id修改阶段信息
	 *
	 * @param tbStageMainEntity
	 * @return
	 */
	@Override
	public boolean update(TbStageMainEntity tbStageMainEntity) {
		LambdaUpdateWrapper<TbStageMainEntity> wrapper = Wrappers.<TbStageMainEntity>lambdaUpdate();
		wrapper.eq(TbStageMainEntity::getStageId, tbStageMainEntity.getStageId());
		return update(tbStageMainEntity, wrapper);
	}

	/**
	 * 根据阶段id获取信息
	 *
	 * @param pageQuery
	 * @return
	 */
	@Override
	public Page<TbStageMainEntity> pageByTrialId(PageQuery<TbStageMainEntity> pageQuery) {
		if (StringUtil.isEmpty(pageQuery.getData().getTrialId())) return null;
		Page<TbStageMainEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		/*LambdaQueryWrapper<TbStageMainEntity> wrapper = Wrappers.<TbStageMainEntity>lambdaQuery();
		wrapper.eq(TbStageMainEntity::getTrialId, pageQuery.getData().getTrialId());
		wrapper.orderByAsc(TbStageMainEntity::getSort);
		return this.page(page, wrapper);*/
		return stageMainMapper.pageByTrialId(page, pageQuery.getData().getTrialId());

	}

	/**
	 * @return List<ResultVo>
	 * @Description 根据试验id获取流程阶段id  和流程阶段名称
	 * @Date 2023/4/4 10:41
	 * @Param trialId
	 */
	@Override
	public List<ResultVo> getByTrialId(String trialId) {
		List<ResultVo> resultVos = new ArrayList<>();
		List<TbStageMainEntity> list = this.list();
		if (list.isEmpty()) return null;
		list.forEach(i -> {
			resultVos.add(new ResultVo(i.getStageId(), i.getStageName()));
		});
		return resultVos;
	}

	/**
	 * @return List<TbStageMainEntity>
	 * @Description 通过试验id获取流程阶段列表
	 * @Date 2023/4/5 13:34
	 * @Param trialId
	 */
	@Override
	public List<TbStageMainEntity> listByTrialId(String trialId) {
		/*LambdaQueryWrapper<TbStageMainEntity> wrapper = Wrappers.<TbStageMainEntity>lambdaQuery();
		wrapper.eq(TbStageMainEntity::getTrialId, trialId);
		wrapper.orderByAsc(TbStageMainEntity::getSort);
		return this.list(wrapper);*/
		return stageMainMapper.listByTrialId(trialId);
	}

	@Override
	public List<TbStageMainEntity> selectAll() {
		return stageMainMapper.selectAll();
	}


}
